import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jdflutter/flutter_jdshop/services/ScreenAdaper.dart';
import 'package:jdflutter/flutter_jdshop/services/SearchServices.dart';
class SearchPage extends StatefulWidget {
  //const SearchPage({Key? key}) : super(key: key);

  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  //保存历史搜索记录  删除历史记录  清空历史记录 长按删除
  List _historyListData = [];
  var _keywords;  // 关键字

  @override
  void initState(){
    super.initState();
    this._getHistroyData();
  }

  // 获取本地存储数据
  _getHistroyData() async{
    var _historyListData = await SearchServices.getHistoryList();
    setState(() {
      this._historyListData = _historyListData;
    });
  }

  // 弹出框
  _showAlertDialog(keywords) async{
    var result = await showDialog(
        context: context,
        builder: (context){
          return AlertDialog(
            title: Text("提示信息！"),
            content: Text("您确定要删除吗？"),
            actions: [
              TextButton(
                  onPressed: (){
                    print("取消");
                    Navigator.pop(context,'Cancle');
                  }, child: Text("取消")),
              TextButton(
                  onPressed: () async{
                    // 注意异步
                    await SearchServices.removeHistoryData(keywords);
                    this._getHistroyData();
                    Navigator.pop(context,'OK');
                  }, child: Text("确定")),
            ],
          );
        }
    );
  }

  // 历史记录 列表
  Widget _historyListWidget(){
    if(_historyListData.length > 0){
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            child: Text("历史记录",style: Theme.of(context).textTheme.subtitle1,),
          ),
          Divider(),
          Column(
            children: this._historyListData.map((value){
              return Column(
                children: [
                  ListTile(
                    title:Text("${value}"),
                    onLongPress: (){
                      this._showAlertDialog("${value}");
                    },
                  ),
                  Divider()
                ],
              );
            }).toList(),
          ),
          SizedBox(height: 100),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              InkWell(
                onTap: (){
                  SearchServices.clearHistoryList();
                  this._getHistroyData();
                },
                child: Container(
                  width: ScreenAdaper.width(400),
                  height: ScreenAdaper.height(64),
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.black45,width: 1)
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(Icons.delete),
                      Text("清空历史记录")
                    ],
                  ),
                ),
              )
            ],
          )
        ],
      );

    }else{
      return Text("");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Container(
          child: TextField(
            autofocus: true,
            decoration: InputDecoration(
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(30),
                borderSide: BorderSide.none
              )
            ),
            onChanged:(value){
              this._keywords = value;
            }
          ),
          height: ScreenAdaper.height(68),
          decoration: BoxDecoration(
            color: Color.fromRGBO(233, 233, 233, 0.8),
            borderRadius: BorderRadius.circular(30)
          ),
        ),
        actions: [
          InkWell(
            child: Container(
              height: ScreenAdaper.height(68),
              width: ScreenAdaper.width(80),
              child: Row(
                children: [
                  Text("搜索")
                ],
              ),
            ),
            onTap: (){
              Navigator.pushReplacementNamed(context, '/productList',arguments: {
                'keywords':this._keywords
              });
            },
          )
        ],
      ),
      body: Container(
        padding: EdgeInsets.all(10),
        child: ListView(
          children: [
            Container(
              child: Text("热搜",style: Theme.of(context).textTheme.subtitle1,),
            ),
            Divider(),
            Wrap(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(233, 233, 233, 0.9),
                    borderRadius: BorderRadius.circular(10)
                  ),
                  child: Text("女装"),
                ),
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(10)),
                  child: Text("女装"),
                ),
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(10)),
                  child: Text("笔记本电脑"),
                ),
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(10)),
                  child: Text("女装111"),
                ),
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(10)),
                  child: Text("女装"),
                ),
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(10)),
                  child: Text("女装"),
                ),
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(10)),
                  child: Text("女装"),
                )
              ],
            ),
            SizedBox(height: 10),
            //历史记录
            _historyListWidget()


        ],

        ),
      )
    );
  }
}
